  package com.ape.mapsa.api.rest;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.SecurityContext;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ape.mapsa.api.model.AsignmentValue;
import com.ape.mapsa.api.model.MaquinaAsignment;
import com.ape.mapsa.produccion.model.Extrusion;
import com.ape.mapsa.produccion.model.Maquina;
import com.ape.mapsa.produccion.service.IProduccionService;

@Service
@Path("/maquinas/")
public class MaquinasEndpoint {

	private static final Logger logger = Logger.getLogger(MaquinasEndpoint.class);

	@Context
	SecurityContext sc;
	@Context
	HttpHeaders request;

	@Autowired
	IProduccionService prodService;

	@GET
	@Path("/maquinas-asign")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Produces(MediaType.APPLICATION_JSON)
	public List<MaquinaAsignment> idbases(@QueryParam("tipoMaquina") String maquinaType) {
		logger.debug("getting maquinas de tipo: " + maquinaType);
		List<Maquina> maquinas = prodService.getMaquinasByType(Integer.valueOf(maquinaType));
		List<MaquinaAsignment> maquinasJsonList = new ArrayList<MaquinaAsignment>();
		int id = 0;
		String maquinaCode = "";
		logger.debug("Cant maquinas: " + maquinas.size());
		for (Maquina maquina : maquinas) {
			if (maquinaCode.equals("")) {
				id++;
				maquinaCode = maquina.getMaqCodigo();
			} else if (!maquinaCode.equals("") && !maquinaCode.equals(maquina.getMaqCodigo())) {
				id++;
				maquinaCode = maquina.getMaqCodigo();
			}
			MaquinaAsignment ma = new MaquinaAsignment();
			ma.setName(maquina.getMaqCodigo());
			ma.setDesc(maquina.getDescription());
			List<AsignmentValue> values = new ArrayList<AsignmentValue>();
			List<Extrusion> assignations = prodService.getAssignationByMaquina(maquina.getId());
			logger.info("asignados: " + assignations.size());
			for (Extrusion maquinaAsignment : assignations) {
				AsignmentValue av = new AsignmentValue();
				av.setId("" + id);
				av.setFrom("/Date(" + maquinaAsignment.getFechaInicio().getTime() + ")/");
				av.setTo("/Date(" + maquinaAsignment.getFechaFin().getTime() + ")/");
				av.setDesc("Orden de producción: " + maquinaAsignment.getOrdenProduccion().getId());
				av.setLabel("OP: " + maquinaAsignment.getOrdenProduccion().getId());
				av.setCustomClass("ganttOrange");
				values.add(av);
			}
			if(values.isEmpty()){
				AsignmentValue av = new AsignmentValue();
				av.setId("" + id);
				long today = new Date().getTime();
				av.setFrom("/Date(" + today + ")/");
				av.setTo("/Date(" + today + ")/");
				av.setDesc("");
				av.setLabel("");
				av.setCustomClass("");
				values.add(av);
			}
			ma.setValues(values);
			maquinasJsonList.add(ma);
		}
		return maquinasJsonList;
	}

}
